class Solution {
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if (A == null || B == null) {
            return false;
        }
        return equals(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
    }

    // 判断这两个节点是否相同（node2属于node1的一部分）
    public boolean equals(TreeNode node1, TreeNode node2) {
        if (node2 == null) return true;
        if (node1 == null) return false;
        return node1.val == node2.val && equals(node1.left, node2.left) && equals(node1.right, node2.right);
    }
}
